package com.zhuhjay.boot.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuhjay.boot.mapper.CheckItemMapper;
import com.zhuhjay.domain.entity.CheckItem;
import com.zhuhjay.domain.http.PageResult;
import com.zhuhjay.domain.http.QueryPageBean;
import com.zhuhjay.service.CheckItemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ZhuHJay
 * @date 2022/4/1 19:51
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem> implements CheckItemService {

    @Override
    public void add(CheckItem checkItem) {
        baseMapper.insert(checkItem);
    }

    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {
        Page<CheckItem> checkItemPage = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
        if( queryString != null && queryString.trim().length() > 0 ){
            queryWrapper
                    .eq("code", queryString)
                    .or()
                    .eq("name", queryString);
        }
        IPage<CheckItem> selectPage = baseMapper.selectPage(checkItemPage, queryWrapper);
        return new PageResult(selectPage.getTotal(), selectPage.getRecords());
    }

    @Override
    public void deleteItemById(Integer id) {
        // 查找是否关联检查组
        Long count = baseMapper.findCountByCheckItemId(id);
        if( count > 0 ){
            throw new RuntimeException();
        }
        baseMapper.deleteById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        baseMapper.updateById(checkItem);
    }
}
